package com.zzh.bc.modules.oss;

import cn.hutool.Hutool;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.zzh.bc.utils.FileManager;
import com.zzh.bc.utils.RequestParamUtils;
import com.zzh.bc.utils.RespParamUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;

/**
 * 文件存储
 */
@WebServlet
@MultipartConfig
public class OSSFileServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        RequestParamUtils.logReqParams(req);

        String accept = req.getHeader("accept");
        String contentType = req.getHeader("content-type");
        String publicFilePath = FileManager.getPublicFilePath(req.getServletContext());
        log("文件存储路径：" + publicFilePath);


        if (StringUtils.equals("application/x-java-keystore", accept) && StringUtils.contains(contentType, "multipart/form-data;")) {

            ServletInputStream inputStream = req.getInputStream();
            String fileRawType = FileManager.getFileRawType(inputStream);
            System.out.println("文件类型：" + fileRawType);

            File file = new File(FileManager.FILE_ROOT_PATH, System.currentTimeMillis() + ".jks");
            FileUtils.copyToFile(inputStream, file);

            RespParamUtils.respJsonOk(resp, file.getPath());
            //File dest = new File("file/");
            //inputStream.read()

        } else if (StringUtils.equals("application/vnd.android.package-archive", accept) && StringUtils.contains(contentType, "multipart/form-data;")) {
            Part reqPart = req.getPart("file");
            //保存文件
            File file = new File(publicFilePath, reqPart.getSubmittedFileName());
            //删除原来的文件
            FileManager.safeDeleteConflictFile(file);
            //将输入流保存到文件中
            reqPart.write(file.getAbsolutePath());

            String fileRawType = FileManager.getFileRawType(new FileInputStream(file));
            log("文件类型：" + fileRawType);

            String path = file.getPath().replace(FileManager.getPublicProjectPath(req.getServletContext()), "");
            RespParamUtils.respJsonOk(resp, path);
        } else {
            ServletInputStream inputStream = req.getInputStream();
            String fileRawType = FileManager.getFileRawType(inputStream);
            System.out.println("文件类型：" + fileRawType);
            RespParamUtils.respJsonError(resp, "请求错误");
        }


    }

}
